package com.epfresh.api.widget.list;


import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.api.R;

public class LoadMoreListViewFooter extends LinearLayout {
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_LOADING = 2;
	public final static int STATE_NO_MORE = 3;
	public final static int STATE_LAST_STATUS = 4;
	public final static int STATE_NO_STATUS = 5;
	public final static int STATE_NEXT = 6;

	private Context mContext;

	private View mContentView;
	private View mProgressBar;
	public TextView mHintView;
	public ImageView imgNext;

	private OnNextClickListener onNextClickListener;
	
	public LoadMoreListViewFooter(Context context) {
		super(context);
		initView(context);
	}
	
	public LoadMoreListViewFooter(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public void setOnNextClickListener(OnNextClickListener onNextClickListener) {
		this.onNextClickListener = onNextClickListener;
	}

	public void setState(int state) {
//	    mContentView.setBackgroundColor(Color.parseColor("#ffffff"));
		imgNext.setVisibility(GONE);
		mHintView.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.GONE);
		mHintView.setVisibility(View.GONE);
		mHintView.setOnClickListener(null);
		imgNext.setOnClickListener(null);
		if (state == STATE_READY) {
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlistview_footer_hint_ready);
		} else if (state == STATE_LOADING) {
			mProgressBar.setVisibility(View.VISIBLE);
		} else if (state == STATE_NO_MORE) {
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlistview_footer_hint_no_more);
		}else if (state == STATE_NEXT){
		    mContentView.setBackgroundColor(Color.parseColor("#efefef"));
			mHintView.setText("下一个分类");
			imgNext.setVisibility(VISIBLE);
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (onNextClickListener != null)
						onNextClickListener.onNextClick();
				}
			});
			imgNext.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (onNextClickListener != null)
						onNextClickListener.onNextClick();
				}
			});
		} else if (state == STATE_LAST_STATUS) {
			mHintView.setVisibility(View.VISIBLE);
//			mHintView.setText(R.string.xlistview_footer_hint_normal);
		} else  if (state == STATE_NO_STATUS) {

		} else {
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlistview_footer_hint_normal);
		}
	}
	
	public void setBottomMargin(int height) {
		if (height < 0) return ;
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
		lp.bottomMargin = height;
		mContentView.setLayoutParams(lp);
	}

	public int getBottomMargin() {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
		return lp.bottomMargin;
	}

	/**
	 * normal status
	 */
	public void normal() {
		mHintView.setVisibility(View.VISIBLE);
		mProgressBar.setVisibility(View.GONE);
	}


	/**
	 * loading status
	 */
	public void loading() {
		mHintView.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.VISIBLE);
	}

	/**
	 * hide footer when disable pull load more
	 */
	public void hide() {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
		lp.height = 0;
		mContentView.setLayoutParams(lp);
	}

	/**
	 * show footer
	 */
	public void show() {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
		lp.height = LayoutParams.WRAP_CONTENT;
		mContentView.setLayoutParams(lp);
	}

	private void initView(Context context) {
		mContext = context;
		LinearLayout moreView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.xlistview_footer, null);
		addView(moreView);
		moreView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		
		mContentView = moreView.findViewById(R.id.xlistview_footer_content);
		mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar);
		mHintView = (TextView)moreView.findViewById(R.id.xlistview_footer_hint_textview);
		imgNext = moreView.findViewById(R.id.img_next);

	}

	public interface OnNextClickListener {
		void onNextClick();
	}

}
